package com.chunyu.ask.service;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.chunyu.ask.bean.Channel;
import com.chunyu.ask.mapper.ChannelMapper;
import com.chunyu.ask.util.string.StringTool;

@Service
public class ChannelService {
	@Autowired
	private ChannelMapper channelMapper;
	
	/**
	 * 通过id取频道内容
	 * @param id
	 * @return
	 */
	public Channel getById(int id){
		return channelMapper.getById(id);
	}
	
	/**
	 * 根据map去频道列表
	 * @param paramMap
	 * @return
	 */
	public List<Channel> getList(Map<String, Object> paramMap){
		//默认不传参数，显示20列
		if(StringUtils.isEmpty(StringTool.getMapString(paramMap, "firstResult"))){
			paramMap.put("firstResult", 0);
		}
		if(StringUtils.isEmpty(StringTool.getMapString(paramMap, "maxResult"))){
			paramMap.put("maxResult", 20);
		}
		return channelMapper.getList(paramMap);
	}
	
	/**
	 * 分页-得到列表数目
	 * @param paramMap
	 * @return
	 */
	public int getcunt(Map<String, Object> paramMap){
		return channelMapper.getCount(paramMap);
	}
	
	/**
	 * 根据map得到——>频道单个对象
	 * @param paramMap
	 * @return
	 */
	public Channel getChannel(Map<String, Object> paramMap){
		Channel obj=null;
		paramMap.put("firstResult", "0");
		paramMap.put("maxResult", "1");
		List<Channel> channel= channelMapper.getList(paramMap);
		if(!CollectionUtils.isEmpty(channel)){
			obj = channel.get(0);
		}
		return obj;
	}
}
